{% extends 'stocks/base.html' %}

{% block title %}{{ title }}{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col-12">
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{% url 'strategy_dashboard' %}">策略仪表板</a></li>
                    <li class="breadcrumb-item"><a href="{% url 'strategy_list' %}">策略列表</a></li>
                    <li class="breadcrumb-item active">{{ strategy.name }}</li>
                </ol>
            </nav>
            <h2>{{ strategy.name }}</h2>
            <p class="text-muted">{{ strategy.description }}</p>
        </div>
    </div>

    <div class="row">
        <!-- 策略基本信息 -->
        <div class="col-md-6">
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="card-title mb-0">策略信息</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-6">
                            <strong>策略类型:</strong><br>
                            <span class="badge bg-primary">{{ strategy.get_strategy_type_display }}</span>
                        </div>
                        <div class="col-6">
                            <strong>状态:</strong><br>
                            <span class="badge {% if strategy.is_active %}bg-success{% else %}bg-secondary{% endif %}">
                                {% if strategy.is_active %}启用{% else %}禁用{% endif %}
                            </span>
                        </div>
                    </div>
                    <hr>
                    <div class="row">
                        <div class="col-12">
                            <strong>参数配置:</strong>
                            <div class="mt-2 p-3 bg-light rounded">
                                {% if strategy.parameters %}
                                    <div class="row">
                                        {% for key, value in strategy.parameters.items %}
                                            <div class="col-md-6 mb-2">
                                                <strong>{{ key }}:</strong> 
                                                <span class="badge bg-info">{{ value }}</span>
                                            </div>
                                        {% endfor %}
                                    </div>
                                {% else %}
                                    <p class="text-muted mb-0">无参数配置</p>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                    <div class="row mt-3">
                        <div class="col-12">
                            <strong>创建时间:</strong> {{ strategy.created_at|date:"Y-m-d H:i" }}<br>
                            <strong>更新时间:</strong> {{ strategy.updated_at|date:"Y-m-d H:i" }}
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 策略表现统计 -->
        <div class="col-md-6">
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="card-title mb-0">30天表现统计</h5>
                </div>
                <div class="card-body">
                    {% if performance_30d.error %}
                        <div class="alert alert-warning">
                            {{ performance_30d.error }}
                        </div>
                    {% else %}
                        <div class="row text-center">
                            <div class="col-4">
                                <div class="display-6 text-primary">{{ performance_30d.total_signals }}</div>
                                <small class="text-muted">总信号数</small>
                            </div>
                            <div class="col-4">
                                <div class="display-6 text-info">{{ performance_30d.avg_strength|floatformat:1 }}</div>
                                <small class="text-muted">平均强度</small>
                            </div>
                            <div class="col-4">
                                <div class="display-6 text-success">{{ performance_30d.period_days }}</div>
                                <small class="text-muted">统计天数</small>
                            </div>
                        </div>
                    {% endif %}
                </div>
            </div>

            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="card-title mb-0">90天表现统计</h5>
                </div>
                <div class="card-body">
                    {% if performance_90d.error %}
                        <div class="alert alert-warning">
                            {{ performance_90d.error }}
                        </div>
                    {% else %}
                        <div class="row text-center">
                            <div class="col-4">
                                <div class="display-6 text-primary">{{ performance_90d.total_signals }}</div>
                                <small class="text-muted">总信号数</small>
                            </div>
                            <div class="col-4">
                                <div class="display-6 text-info">{{ performance_90d.avg_strength|floatformat:1 }}</div>
                                <small class="text-muted">平均强度</small>
                            </div>
                            <div class="col-4">
                                <div class="display-6 text-success">{{ performance_90d.period_days }}</div>
                                <small class="text-muted">统计天数</small>
                            </div>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <!-- 近期信号 -->
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">近期买入信号 (最近30天)</h5>
                </div>
                <div class="card-body">
                    {% if recent_signals %}
                        <div class="table-responsive">
                            <table class="table table-striped table-hover">
                                <thead>
                                    <tr>
                                        <th>股票代码</th>
                                        <th>股票名称</th>
                                        <th>信号日期</th>
                                        <th>信号价格</th>
                                        <th>信号强度</th>
                                        <th>买入理由</th>
                                        <th>创建时间</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for signal in recent_signals %}
                                        <tr>
                                            <td>{{ signal.symbol }}</td>
                                            <td>{{ signal.stock_name }}</td>
                                            <td>{{ signal.signal_date|date:"Y-m-d" }}</td>
                                            <td>¥{{ signal.signal_price|floatformat:2 }}</td>
                                            <td>
                                                <span class="badge {% if signal.signal_strength >= 80 %}bg-danger{% elif signal.signal_strength >= 60 %}bg-warning{% else %}bg-info{% endif %}">
                                                    {{ signal.signal_strength|floatformat:1 }}
                                                </span>
                                            </td>
                                            <td>{{ signal.reason }}</td>
                                            <td>{{ signal.created_at|date:"Y-m-d H:i" }}</td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    {% else %}
                        <div class="alert alert-info">
                            <i class="fas fa-info-circle"></i> 最近30天内未发现该策略的买入信号。
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <!-- 操作按钮 -->
    <div class="row mt-4">
        <div class="col-12">
            <div class="d-flex gap-2">
                <a href="{% url 'strategy_list' %}" class="btn btn-secondary">
                    <i class="fas fa-arrow-left"></i> 返回策略列表
                </a>
                <a href="{% url 'buy_signals' %}?strategy={{ strategy.id }}" class="btn btn-primary">
                    <i class="fas fa-search"></i> 查看所有信号
                </a>
                <button class="btn btn-outline-primary" onclick="analyzeWithThisStrategy()">
                    <i class="fas fa-cogs"></i> 使用此策略分析
                </button>
            </div>
        </div>
    </div>
</div>

<script>
function analyzeWithThisStrategy() {
    const symbol = prompt('请输入要分析的股票代码:');
    if (symbol) {
        window.location.href = `/stocks/analyze/${symbol}/?strategy_id={{ strategy.id }}`;
    }
}
</script>
{% endblock %}
